package polygon.util;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author Mike Mirzayanov
 */
public class StringUtil {
    public static boolean isEmpty(String s) {
        return s == null || s.isEmpty();
    }

    public static String stripSpaces(String s) {
        if (s == null) {
            return null;
        }
        
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ' ') {
                sb.append(s.charAt(i));
            }
        }
        return sb.toString();
    }

    public static Set<Integer> parseIntegers(String s) {
        if (s == null) {
            return new TreeSet<Integer>();
        } else {
            try {
                Set<Integer> integers = new TreeSet<Integer>();
                String[] toks = s.split("[,]+");
                for (String token : toks) {
                    String tok = token.trim();
                    if (!tok.isEmpty()) {
                        String[] tt = tok.split("-");
                        if (tt.length == 0 || tt.length > 2) {
                            throw new IllegalArgumentException("Illegal integer-list: \"" + s + "\".");
                        }
                        if (tt.length == 1) {
                            integers.add(Integer.parseInt(tt[0]));
                        } else {
                            int from = Integer.parseInt(tt[0]);
                            int to = Integer.parseInt(tt[1]);

                            if (from > to) {
                                throw new IllegalArgumentException("Illegal integer-list: \"" + s + "\".");
                            }

                            for (int i = from; i <= to; i++) {
                                integers.add(i);
                            }
                        }
                    }
                }
                return integers;
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Illegal integer-list: \"" + s + "\".");
            }
        }
    }

    public static String formatIntegers(List<Integer> integers) {
        StringBuilder sb = new StringBuilder();

        Set<Integer> set = new TreeSet<Integer>(integers);

        int prev = Integer.MIN_VALUE;
        int count = 0;

        for (Integer integer : set) {
            if (integer != prev + count) {
                if (prev != Integer.MIN_VALUE) {
                    if (count > 1) {
                        sb.append(",").append(prev + "-" + (prev + count - 1));
                    } else {
                        sb.append(",").append(prev);
                    }
                }

                prev = integer;
                count = 1;
            } else {
                count++;
            }
        }

        if (count > 0) {
            if (count > 1) {
                sb.append(",").append(prev + "-" + (prev + count - 1));
            } else {
                sb.append(",").append(prev);
            }
        }

        return sb.length() == 0 ? "" : sb.substring(1);
    }

    public static String quoteHtml(String html) {
        return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    }

    public static String formatComments(String comment) {
        String[] tokens = comment.split("\\[pre\\]|\\[/pre\\]");
        StringBuilder sb = new StringBuilder();
        boolean inside = false;
        for (String token : tokens) {
            if (inside) {
                sb.append("<pre style='display:inline;'>" + quoteHtml(token) + "</pre>");
            } else {
                sb.append(quoteHtml(token).replaceAll("[\n\r][\n\r]+", "<br/>"));
            }
            inside ^= true;
        }
        return sb.toString(); 
    }
}
